package net.easyjava.framework.third.letv;

import net.easyjava.framework.utils.ProjectProperties;
import net.easyjava.third.letv.LetvLiveClient;
import net.easyjava.third.letv.LetvVideoClient;
import net.easyjava.third.letv.bean.LetvUploadData;
import net.easyjava.third.letv.bean.LetvVideo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by wxnacy on 16/8/8.
 */
public class LetvUtils {
    private static Logger logger = LoggerFactory.getLogger(LetvUtils.class);
    private static LetvVideoClient videoClient;
    private static LetvLiveClient liveClient;



    /**
     * 获取视频上传初始化信息
     * @param videoName
     * @return
     */
    public static LetvUploadData initUpload(String videoName) {
        try {
            initVideo();
            LetvUploadData uploadData = videoClient.getUploadUrl(videoName, 1);
            return uploadData;
        } catch (IOException e) {
            logger.error("乐视获取视频上传初始化信息出错 videoName:{}",videoName);
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }
    }

    /**
     * 获取视频信息
     * @param videoId
     * @return
     */
    public static LetvVideo getVideo(String videoId){
        try {
            initVideo();
            LetvVideo letvVideo = videoClient.getVideo(videoId);
            return letvVideo;
        } catch (Exception e) {
            logger.error("乐视获取视频信息出错 videoId:{}",videoId);
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }
    }



    /**
     * 初始化视频client
     */
    private static void initVideo(){
        if(videoClient == null){
            initVideoClient();
        }
    }
    private static synchronized void initVideoClient(){
        if(videoClient == null){
            ProjectProperties properties = ProjectProperties.getInstance();
            String userUnique = properties.getProperty("letv.user.unique");
            String secretKey = properties.getProperty("letv.secret.key");
            videoClient = LetvVideoClient.getInstance(userUnique,secretKey);
            logger.debug("letv video client init ");
        }
    }

    /**
     * 初始化直播client
     */
    private static void initLive(){
        if(liveClient == null){
            initLiveClient();
        }
    }
    private static synchronized void initLiveClient(){
        if(liveClient == null){
            ProjectProperties properties = ProjectProperties.getInstance();
            String userUnique = properties.getProperty("letv.user.unique");
            String secretKey = properties.getProperty("letv.secret.key");
            liveClient = LetvLiveClient.getInstance(userUnique,secretKey);
            logger.debug("letv live client init ");
        }
    }


}
